﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：公司信息
//********************************************************************************//
using System;
using System.Web.UI;
using Entity;
using BLL;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.JiChuXX
{
    public partial class GongSiXX : BasePage
    {
        #region 属性

        /// <summary>
        ///当前操作项ID
        /// </summary>
        private int GongSiXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_GongSiXXID"]);
            }
            set
            {
                ViewState["_GongSiXXID"] = value;
            }
        }
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 公司信息对象
        /// </summary>
        YS_JX_GONGSIXX _YS_JX_GONGSIXX = new YS_JX_GONGSIXX();

        /// <summary>
        /// 公司信息BLL
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                
                //SetData();
                //Display();
            }
            LoadData(true);
            //LoadData(false);
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {

            base.Render(sender);
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            /*添加与修改均为下月数据版本
            if (e.Parameter == "save")
            {
                OperateState operateState;
                //添加
                if (id < 0)
                {
                    //判断编号是否重复
                    if (_YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("S_BIANHAO='{0}'", this.txtBianHao.Text), null).Rows.Count > 0)
                    {
                        e.Result = e.Parameter + "|-1|你所输入的公司编号,系统中已经存在使用过,请修改。";
                        return;
                    }
                    operateState = OperateState.Add;
                    _YS_JX_GONGSIXX.ID = 0;
                    _YS_JX_GONGSIXX.I_JLZT = 0;
                    //添加及修改均是操作下月的数据
                    _YS_JX_GONGSIXX.I_LEIXING = 1;
                }
                else
                {
                    //修改操作
                    operateState = OperateState.Modify;
                    //为当前月时 修改操作获取下一月记录进行修改
                    if (this.conNianYu.SelectedIndex == 0)
                    {
                        _YS_JX_GONGSIXX = BusinessTool.GetNextDataEntityByID<YS_JX_GONGSIXX>(id, "YS_JX_GONGSIXX", "S_BIANHAO");
                    }
                    else if (this.conNianYu.SelectedIndex == 1)//为下月时 即修改当前记录
                    {
                        _YS_JX_GONGSIXX = this._YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(id);
                    }
                }
                //从页面获取属性值
                _YS_JX_GONGSIXX.S_BIANHAO = this.txtBianHao.Text;
                _YS_JX_GONGSIXX.S_MINGCHENG = this.txtMingCheng.Text;
                _YS_JX_GONGSIXX.S_DIZHI = this.txtDiZ.Text;
                _YS_JX_GONGSIXX.S_YOUBIAN = this.txtYouBian.Text;
                _YS_JX_GONGSIXX.S_LIANXIDH = this.txtLianXiDH.Text;
                _YS_JX_GONGSIXX.S_LIANXIBM = this.txtLianXiBM.Text;
                _YS_JX_GONGSIXX.S_LIANXIR = this.txtLianXiR.Text;
                _YS_JX_GONGSIXX.S_BEIZHU = this.txtBeiZhu.Text;
                _YS_JX_GONGSIXX.S_JUBIANH = Convert.ToString(this.ddlJuBH.Value);
                _YS_JX_GONGSIXX.S_ZHANDIANBH = Convert.ToString(this.conZhanDian.Value);
                //设置操作信息
                this._YS_JX_GONGSIXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                this._YS_JX_GONGSIXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                //执行操作
                if (_YS_JX_GONGSIXXBLL.InsertOrUpdate(_YS_JX_GONGSIXX, null))
                {
                    _SYS_LOGBLL.Log<YS_JX_GONGSIXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "公司信息", operateState, _YS_JX_GONGSIXX, null);
                    e.Result = e.Parameter + "|0|保存成功！";
                }
            }*/
            /*选中当前月 修改本月及下月 选中下月 仅修改下月*/
            if (e.Parameter == "save")
            {
                OperateState operateState;
                //添加
                if (id < 0)
                {
                    //判断编号是否重复
                    if (_YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("S_BIANHAO='{0}'", this.txtBianHao.Text), null).Rows.Count > 0)
                    {
                        e.Result = e.Parameter + "|-1|你所输入的公司编号,系统中已经存在使用过,请修改。";
                        return;
                    }
                    operateState = OperateState.Add;
                    _YS_JX_GONGSIXX.ID = 0;
                    _YS_JX_GONGSIXX.I_JLZT = 0;
                    //添加及修改均是操作下月的数据
                    _YS_JX_GONGSIXX.I_LEIXING = 1;
                }
                else
                {
                    //修改操作
                    operateState = OperateState.Modify;
                    _YS_JX_GONGSIXX = this._YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(id);
                }
                //从页面获取属性值
                _YS_JX_GONGSIXX.S_BIANHAO = this.txtBianHao.Text;
                _YS_JX_GONGSIXX.S_MINGCHENG = this.txtMingCheng.Text;
                _YS_JX_GONGSIXX.S_DIZHI = this.txtDiZ.Text;
                _YS_JX_GONGSIXX.S_YOUBIAN = this.txtYouBian.Text;
                _YS_JX_GONGSIXX.S_LIANXIDH = this.txtLianXiDH.Text;
                _YS_JX_GONGSIXX.S_LIANXIBM = this.txtLianXiBM.Text;
                _YS_JX_GONGSIXX.S_LIANXIR = this.txtLianXiR.Text;
                _YS_JX_GONGSIXX.S_BEIZHU = this.txtBeiZhu.Text;
                _YS_JX_GONGSIXX.S_JUBIANH = Convert.ToString(this.ddlJuBH.Value);
                _YS_JX_GONGSIXX.S_ZHANDIANBH = Convert.ToString(this.conZhanDian.Value);
                //设置操作信息
                this._YS_JX_GONGSIXX.I_XIUGAIR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_GONGSIXX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_GONGSIXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                if (operateState == OperateState.Add)
                {
                    _SYS_LOGBLL.Log<YS_JX_GONGSIXX>(LoginInfo.CurrentUser,BusinessTool.WType.ToString(), "公司信息", operateState, _YS_JX_GONGSIXX, null);
                    _YS_JX_GONGSIXXBLL.InsertOrUpdate(_YS_JX_GONGSIXX);
                }
                else
                {
                    //如果修改
                    _YS_JX_GONGSIXXBLL.UpdateByLeixing(_YS_JX_GONGSIXX, this.conNianYu.Text == "下一月" ? (int?)1 : null);
                }
                
                e.Result = e.Parameter + "|0|保存成功！";
            }
            else if (e.Parameter == "deleteValidate")
            {
                //获取当前操作项
                _YS_JX_GONGSIXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(id, null);
                //确认是否有用户使用
                if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_gongsibh='{0}'", _YS_JX_GONGSIXX.S_BIANHAO), null).Rows.Count > 0)
                {
                    e.Result = e.Parameter + "|-1|当前公司系统正在使用,不允许删除。";
                }
                else
                {
                    e.Result = e.Parameter + "|0|success";
                }
            }
            else if (e.Parameter == "delete")
            {
                //获取当前操作项
                _YS_JX_GONGSIXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(id);
                //更新操作人信息
                this._YS_JX_GONGSIXX.I_XIUGAIR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_JX_GONGSIXX.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                //this._YS_JX_GONGSIXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                //逻辑删除
                this._YS_JX_GONGSIXX.I_JLZT = -1;
                _YS_JX_GONGSIXXBLL.UpdateByLeixing(_YS_JX_GONGSIXX, this.conNianYu.Text == "下一月" ? (int?)1 : null);
                //if (_YS_JX_GONGSIXXBLL.InsertOrUpdate(_YS_JX_GONGSIXX, null))
                //{
                //添加日志
                _SYS_LOGBLL.Log<YS_JX_GONGSIXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "公司信息", OperateState.Delete, _YS_JX_GONGSIXX, null);
                e.Result = e.Parameter + "|0|删除成功！";
                //}
            }
            else if (e.Parameter == "search")
            {
                //要定位的记录局编号
                string conBH = this.conBH.Text.Trim();
                //重新绑定下结果集,否则结果集并不是最新的
                LoadData(true);
                //定位
                if (conBH != "")
                {
                    int index1 = PageTools.SearchRecordIndex(this.gvList, "S_BIANHAO", conBH, false);
                    int index2 = PageTools.SearchRecordIndex(this.gvList, "S_MINGCHENG", conBH, false);
                    if (index1 == -1 && index2 == -1)
                    {
                        e.Result = e.Parameter + "|-1|未找到对应数据！";
                    }
                    else
                    {
                        int rsindex = -1;
                        if (index1 == -1)
                        {
                            rsindex = index2;
                        }
                        else if (index2 == -1)
                        {
                            rsindex = index1;
                        }
                        else
                        {
                            rsindex = index1 < index2 ? index1 : index2;
                        }
                        e.Result = e.Parameter + "|0|" + rsindex;
                    }
                }
            }
            else if (e.Parameter == "setinfo")
            {
                string msg = "";
                if (this.conNianYu.SelectedIndex < 2)
                {
                    _YS_JX_GONGSIXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(id);
                    msg = string.Join("|", this._YS_JX_GONGSIXX.S_BIANHAO, this._YS_JX_GONGSIXX.S_MINGCHENG, this._YS_JX_GONGSIXX.S_DIZHI, this._YS_JX_GONGSIXX.S_JUBIANH, this._YS_JX_GONGSIXX.S_YOUBIAN, this._YS_JX_GONGSIXX.S_LIANXIDH, this._YS_JX_GONGSIXX.S_LIANXIBM, this._YS_JX_GONGSIXX.S_LIANXIR, this._YS_JX_GONGSIXX.S_BEIZHU);
                }
                else
                {
                    DataRow drtemp = BusinessTool.GETDATAHISTORY("YS_JX_GONGSIXX", this.conLiShiNY.Text, "ID=" + id).Rows[0];
                    msg = string.Join("|", Convert.ToString(drtemp["S_BIANHAO"]), Convert.ToString(drtemp["S_MINGCHENG"]), Convert.ToString(drtemp["S_DIZHI"]), Convert.ToString(drtemp["S_JUBIANH"]), Convert.ToString(drtemp["S_YOUBIAN"]), Convert.ToString(drtemp["S_LIANXIDH"]), Convert.ToString(drtemp["S_LIANXIBM"]), Convert.ToString(drtemp["S_LIANXIR"]), Convert.ToString(drtemp["S_BEIZHU"]));

                }
                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData(true);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            string zhandianbh = BusinessTool.ZHANDIANBH();
            DataTable dtZhanDian = new DataTable();
            if (zhandianbh == "00")
            {
                dtZhanDian = BusinessTool.GetTextAndValue("YS_JX_ZHANDIAN", "S_ZHANDIANMC", "S_ZHANDIANBH", "", "S_ZHANDIANBH<>00").Tables[0];
            }
            else
            {
                dtZhanDian = BusinessTool.GetTextAndValue("YS_JX_ZHANDIAN", "S_ZHANDIANMC", "S_ZHANDIANBH", "", "S_ZHANDIANBH=" + zhandianbh).Tables[0];
            }
            PageTools.BindComboBox(this.conZhanDian, dtZhanDian);
            this.conZhanDian.SelectedIndex = 0;
            //加载局
            this.ddlJuBH.DataSource = new YS_JX_JUXINXBLL().SelectYS_JX_JUXINXesAll();
            this.ddlJuBH.TextField = "S_MINGCHENG";
            this.ddlJuBH.ValueField = "S_BIANHAO";
            this.ddlJuBH.DataBind();
            this.ddlJuBH.SelectedIndex = 0;
            //获取当前水量年月
            this.hdCollection.Set("YM", BusinessTool.GetWaterYM());
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtBeiZhu.Text = "";
            this.txtBianHao.Text = "";
            this.txtDiZ.Text = "";
            this.txtLianXiBM.Text = "";
            this.txtLianXiDH.Text = "";
            this.txtLianXiR.Text = "";
            this.txtMingCheng.Text = "";
            this.txtYouBian.Text = "";
            this.txtJuBH.Text = "";
            this.ddlJuBH.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool reget)
        {
            if (reget)
            {
                //查询站点下所有局
                string condition = "";
                condition = string.Format("S_ZHANDIANBH='{0}' and i_jlzt=0", this.conZhanDian.Value);
                //查询的是本月或下月
                if (Convert.ToInt32(this.conNianYu.Value) == 0 || Convert.ToInt32(this.conNianYu.Value) == 1)
                {
                    condition = string.Format("{0} and I_LEIXING={1}", condition, this.conNianYu.Value);
                    this.dtSource = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(condition, "S_JUBIANH asc,S_BIANHAO asc");
                }
                else
                {
                    if (this.conLiShiNY.Text == "")
                    {
                        this.dtSource = null;
                    }
                    else
                    {
                        //查询的是历史数据
                        this.dtSource = BusinessTool.GETDATAHISTORY("YS_JX_GONGSIXX", this.conLiShiNY.Text, string.Format("S_ZHANDIANBH='{0}'", this.conZhanDian.Value));
                        if (this.dtSource.Rows.Count == 0) this.dtSource = null;
                    }
                }
            }
            //绑定数据
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        ///// <summary>
        ///// 给输入框赋值
        ///// </summary>
        //private void SetData()
        //{
        //    if (this.gvList.FocusedRowIndex == -1)
        //    {
        //        InitData();
        //        return;
        //    }
        //    this.GongSiXXID = Convert.ToInt32(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "ID"));
        //    this.hdCollection.Set("ID", this.GongSiXXID);
        //    if (this.conNianYu.SelectedIndex < 2)
        //    {
        //        //获取当前操作项
        //        _YS_JX_GONGSIXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXX(this.GongSiXXID, null);
        //        this.txtBianHao.Text = _YS_JX_GONGSIXX.S_BIANHAO;
        //        this.txtMingCheng.Text = _YS_JX_GONGSIXX.S_MINGCHENG;
        //        this.txtDiZ.Text = _YS_JX_GONGSIXX.S_DIZHI;
        //        this.txtYouBian.Text = _YS_JX_GONGSIXX.S_YOUBIAN;
        //        this.txtLianXiDH.Text = _YS_JX_GONGSIXX.S_LIANXIDH;
        //        this.txtLianXiBM.Text = _YS_JX_GONGSIXX.S_LIANXIBM;
        //        this.txtLianXiR.Text = _YS_JX_GONGSIXX.S_LIANXIR;
        //        this.txtBeiZhu.Text = _YS_JX_GONGSIXX.S_BEIZHU;
        //        this.ddlJuBH.Value = _YS_JX_GONGSIXX.S_JUBIANH;
        //        this.txtJuBH.Text = this.ddlJuBH.Value.ToString();
        //    }
        //    else
        //    {
        //        if (this.dtSource != null)
        //        {
        //            foreach (DataRow row in this.dtSource.Rows)
        //            {
        //                if (Convert.ToInt32(row["ID"]) == this.GongSiXXID)
        //                {
        //                    this.txtBianHao.Text = Convert.ToString(row["S_BIANHAO"]);
        //                    this.txtMingCheng.Text = Convert.ToString(row["S_MINGCHENG"]);
        //                    this.txtDiZ.Text = Convert.ToString(row["S_DIZHI"]);
        //                    this.txtYouBian.Text = Convert.ToString(row["S_YOUBIAN"]);
        //                    this.txtLianXiDH.Text = Convert.ToString(row["S_LIANXIDH"]);
        //                    this.txtLianXiBM.Text = Convert.ToString(row["S_LIANXIBM"]);
        //                    this.txtLianXiR.Text = Convert.ToString(row["S_LIANXIR"]);
        //                    this.txtBeiZhu.Text = Convert.ToString(row["S_BEIZHU"]);
        //                    this.ddlJuBH.Value = Convert.ToString(row["S_JUBIANH"]);
        //                    this.txtJuBH.Text = this.ddlJuBH.Value.ToString();
        //                }
        //            }
        //        }
        //    }

        //}

        #endregion
    }
}